package com.sl.hx.shop.ui.mine.presnter;

import android.content.Context;
import android.os.CountDownTimer;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.widget.TextView;

import com.apkfuns.logutils.LogUtils;
import com.sl.hx.shop.R;
import com.sl.hx.shop.base.BasePresenter;
import com.sl.hx.shop.bean.LoginBean;
import com.sl.hx.shop.constant.AppConfig;
import com.sl.hx.shop.http.ApiFactory;
import com.sl.hx.shop.http.HttpUtil;
import com.sl.hx.shop.http.ProgressSubscribe;
import com.sl.hx.shop.ui.mine.activity.LoginActivity;
import com.sl.hx.shop.ui.mine.contacts.LoginContacts;
import com.sl.hx.shop.utils.HMACSHA1;
import com.trello.rxlifecycle2.android.ActivityEvent;

import io.reactivex.Observable;

public class LoginPresenter extends BasePresenter<LoginContacts.view> implements LoginContacts.presenter {

    private static final String KEY = "shengli";
    private String sign="";
    private Context mContext;

    /** 倒计时 */
    private GetIdeCodeCountDownTimer mCountDownTimer;
    /** 验证码的view */
    private TextView mGetIdeCodeView;
    /** 倒计时是否开启 */
    private boolean mIsTimerStart = false;

    public LoginPresenter(Context context) {
        this.mContext=context;
        mCountDownTimer = new GetIdeCodeCountDownTimer(60000, 1000);

    }

    //获取验证码
    public void getCode(TextView mCode, String mPhone) {
        mGetIdeCodeView = mCode;
        Observable observable = ApiFactory.getInstance().getCode(mPhone,HMACSHA1.md5(KEY+mPhone.substring(0,4)));
        HttpUtil.getInstance().toSubscribe(observable, new ProgressSubscribe<String>(mContext) {
            @Override
            protected void _onNext(String s) {

            }

            @Override
            public void onComplete() {
                super.onComplete();
                sendSuccess();
            }

            @Override
            protected void _onError(String message) {
                getView().showtoast(message);
                LogUtils.e(message);
            }
        }, false, getView(), ActivityEvent.DESTROY);
    }


    public void login(final String mPhone, String mYzm) {
        Observable observable = ApiFactory.getInstance().login(mPhone,mYzm,2, AppConfig.CHANNEL);
        HttpUtil.getInstance().toSubscribe(observable, new ProgressSubscribe<LoginBean>(mContext) {
            @Override
            protected void _onNext(LoginBean loginBean) {
                getView().loginSuccess(loginBean,mPhone);
                //Log.e("lodin",loginBean.getId());
            }

            @Override
            protected void _onError(String message) {
                getView().showtoast(message);
            }
        }, true, getView(), ActivityEvent.DESTROY);
    }

    /**
     * 验证码发送成功
     */
    public void sendSuccess() {
        getView().showtoast("验证码已发送");
        setCodeView(false);
        mCountDownTimer.cancel();
        mCountDownTimer.start();
        mIsTimerStart = true;
    }

    private void setCodeView(boolean isAble) {
        if (isAble) {
            mGetIdeCodeView.setEnabled(true);
            mGetIdeCodeView.setTextColor(ContextCompat.getColor(mGetIdeCodeView.getContext(), R.color.yzm));
        }else {
            mGetIdeCodeView.setEnabled(false);
            mGetIdeCodeView.setTextColor(ContextCompat.getColor(mGetIdeCodeView.getContext(), R.color.yzm));
        }

    }

    private class GetIdeCodeCountDownTimer extends CountDownTimer {

        /**
         * 验证码倒计时
         *
         * @param millisInFuture    总时长
         * @param countDownInterval 间隔多长时间
         */
        public GetIdeCodeCountDownTimer(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onTick(long millisUntilFinished) {
            if (mGetIdeCodeView != null) {
                mGetIdeCodeView
                        .setText(String.format(mGetIdeCodeView.getContext().getString(R.string.re_get_identify_code), millisUntilFinished / 1000));
            }
        }

        @Override
        public void onFinish() {
            mIsTimerStart = false;
            setCodeView(true);
            mGetIdeCodeView.setText(mGetIdeCodeView.getContext().getString(R.string.get_identify_code_retry));
        }
    }

    @Override
    public void destroy() {
        if (mCountDownTimer != null) {
            mCountDownTimer.cancel();
            mCountDownTimer = null;
        }
    }

}
